package com.x.windows;


import com.aliyun.alidns20150109.models.DescribeDomainRecordsRequest;
import com.aliyun.alidns20150109.models.DescribeDomainRecordsResponse;
import com.aliyun.alidns20150109.Client;
import com.aliyun.alidns20150109.models.UpdateDomainRecordRequest;
import com.aliyun.alidns20150109.models.UpdateDomainRecordResponse;
import com.aliyun.teaopenapi.models.Config;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

import java.util.Objects;


public class DDnsVerticle extends AbstractVerticle{
  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    super.start(startPromise);
    Config config = new Config();
    config.setAccessKeyId("LTAI5tCc6jzCrV6eMSxrtQSU");
    config.setAccessKeySecret("mVAdJPYdQPiemJxpI2t4DNZPZ0uQPo");
    config.setRegionId("cn-beijing");
    Client client = new Client(config);
    HttpClient client1 = vertx.createHttpClient();
    long id1 = vertx.setPeriodic(1000*60*5,id->{
      vertx.executeBlocking((promise)->{
        DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
        request.domainName="windows.prettyclass.top";
        request.RRKeyWord="@";
        request.typeKeyWord="A";
        try{
          DescribeDomainRecordsResponse response = client.describeDomainRecords(request);
          String recordId = response.getBody().domainRecords.getRecord().get(0).recordId;
          client1.request(HttpMethod.GET,80,"www.3322.org","/dyndns/getip")
            .onComplete(httpClientRequestAsyncResult -> {
              if (httpClientRequestAsyncResult.succeeded()){
                HttpClientRequest request1= httpClientRequestAsyncResult.result();

                request1.putHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.55");
                request1.send(ar2->{
                  if (ar2.succeeded()){
                    HttpClientResponse response1 = ar2.result();

                    response1.handler(buffer -> {
                      String ip = buffer.getString(0,buffer.length());
                      System.out.println(String.format("#############################\n" +
                        "recordId:%s ip:%s\n" +
                        "push begin",recordId,ip));
                      UpdateDomainRecordRequest request2 = new UpdateDomainRecordRequest();
                      if (ip.equals( response.getBody().domainRecords.getRecord().get(0).value)){
                        System.out.println("ip is already synchronization");
                        promise.complete();
                        return;
                      }
                      request2.setRecordId(recordId);
                      request2.setRR("@");
                      request2.setType("A");
                      request2.setValue(ip);
                      try{
                        UpdateDomainRecordResponse recordResponse = client.updateDomainRecord(request2);

                        promise.complete();
                      } catch (Exception e) {
                        promise.complete();
                      }
                    });
                  }
                });
              }
            });
        } catch (Exception e) {
          promise.complete();
        }
      }).onFailure(throwable -> {
        System.out.println(throwable.getMessage());
      });
    });
  }
}
